package files;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;


class Fraction implements Serializable
{
   private static final long serialVersionUID  = 42; 
   String s;
    public Fraction(int n, int d)
    {
        this.n = n;
        this.d = d;
        
        this.reducedForm = this.reduced();
    }
     
    public Fraction(int n, int d, boolean isReduced)
    {
        this.n = n;
        this.d = d;
        if (! isReduced)
            this.reducedForm = this.reduced();
    }  
    Fraction(String string2)
    {
        Scanner s = new Scanner(string2);
        n = s.nextInt();
        d = s.nextInt();
    }
    
    String toString2()
    {
        return n + " " +  d;
    }    
    
    @Override
    public String toString()
    {
        return String.format("%d/%d (=%d/%d)",n,d,reducedForm.n,reducedForm.d);
    }
    
    
    private int n, d;    
    private transient Fraction reducedForm;
    
    private void readObject(ObjectInputStream in)
             throws IOException, ClassNotFoundException {
        in.defaultReadObject();      // Deserialize the component in the usual way.
        reducedForm = this.reduced();        
    }

    private Fraction reduced() {
        int n = Math.max(this.n,this.d), 
            d = Math.min(this.n,this.d);
        while ( n % d != 0)
        {    
            int oldN = n;
            n = d;
            d = oldN % d;            
        }
        return new Fraction(this.n / d, this.d / d, true);
    }
}

public class Files {

    public static void writeFile(File file, List<Fraction> list) throws FileNotFoundException
    {
        PrintStream ps = new PrintStream( file );        
        for (Fraction f : list)
        {
            ps.println( f.toString2() );
        }        
        ps.close();   
    }
    
     public static List<Fraction> readFile(File file) throws FileNotFoundException
    {
        Scanner scanner = new Scanner(file);
        
        List<Fraction> list = new LinkedList<Fraction>();
        while (scanner.hasNextLine())
        {
            String inp = scanner.nextLine();
            Fraction g = new Fraction(inp);
            list.add(g);
            System.out.println(g.toString2());
        }
        scanner.close();   
        return list;
    }
     
    public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
        List<Fraction> list = new LinkedList<>();
        for (int i=1;i<10;i++)
        {
            Fraction f = new Fraction(2*i,i*i);
            list.add(f);
        }
        
        File file = new File("fraction.txt");
// 
//        writeFile(file, list);        
//        list = readFile(file);        
//        
//        for (Fraction f2 : list)         {
//            System.out.println(f2.toString2());
//        }

        //
//        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
//        oos.writeObject(list);
        
        
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
        list = (List<Fraction> )ois.readObject();
       
        for (Fraction f2 : list)     
        {
            System.out.println(f2.toString());
        }
        
    }
}
